<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
    <script src="../../js/lay-module/http/http.js"></script>
</head>
<body onload="init()">
<div class="layui-form layuimini-form">
    <div class="layui-form-item" hidden>
        <label class="layui-form-label required">用户Id</label>
        <div class="layui-input-block">
            <input type="text" name="userId"  id="userId" placeholder="请输入用户帐号" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户帐号</label>
        <div class="layui-input-block">
            <input type="text" name="userAccount"  id="userAccount" placeholder="请输入用户帐号" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" lay-verify="required"  placeholder="请输入用户姓名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" id="phone" name="phone" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户角色</label>
        <div class="layui-input-inline" lay-filter="classDiv">
            <select id="role" name="role" lay-filter="classIdFilter">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户状态</label>
        <div class="layui-input-inline" lay-filter="classDiv">
            <select id="status" name="status" lay-filter="classIdFilter">
                <option>请选择</option>
                <option value="0">正常</option>
                <option value="1">注销</option>
                <option value="2">黑名单</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    function init(){
        var $ = layui.jquery

        // 1.初始化角色下拉框
        $.ajax({
            type: 'GET',
            url: $api + 'role/list',
            contentType: "application/json;charset=utf-8",
            async: false,
            headers: {
                'Authorization': layui.data('table')['Authorization']
            },
            success: function (res) {
                // 对下拉列表进行拼接
                if (!res.code) {
                    html += "<option value=\"\" disabled selected>请选择</option>";
                    for (var i  = 0; i < res.data.length; i++) {
                        html += "<option value=\"" + res.data[i].roleId + "\">" + res.data[i].roleType + "</option>"
                    }
                    // console.log(html);
                    $("select[name='role']").html(html);
                    layui.form.render('select');
                }
            }
        });

        // 2.获取用户当前的信息
        $.ajax({
            type: 'GET',
            url: $api + 'user/select/' + unescape(getQueryString("userId")),
            contentType: "application/json;charset=utf-8",
            async: false,
            headers: {
                'Authorization': layui.data('table')['Authorization']
            },
            success: function (res) {
                // 2.1 根据查询到的数据对页面进行填充
                identifyCode(res)
                if (!res.code) {
                    // 设置<select>当前的选中项
                    var selectRole = 'dd[lay-value=' + res.data.role + ']';
                    var selectStatus = 'dd[lay-value=' + res.data.status + ']';
                    $("#userId").val(res.data.userId);

                    $("#userAccount").val(res.data.userAccount);
                    $("#name").val(res.data.name);
                    $("#phone").val(res.data.phone);
                    $('#role').siblings("div.layui-form-select").find('dl').find(selectRole).click();
                    $('#status').siblings("div.layui-form-select").find('dl').find(selectStatus).click();
                }else{
                    layer.msg(res.message)
                }

            }
        });
    }
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data.field.userId)
            var index = layer.confirm("是否提交", {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    type: 'POST',
                    url: $api + 'user/save',
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    async: false,
                    headers: {
                        'Authorization': layui.data('table')['Authorization']
                    },
                    success: function (res) {
                        // 2.1 根据查询到的数据对页面进行填充
                        identifyCode(res)
                        if (!res.code) {
                            // 关闭弹出层
                            layer.close(index);

                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            // 重新加载父类
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新

                            parent.layer.close(iframeIndex);
                        }else{
                            layer.msg(res.message)
                        }

                    }
                });


            });

            return false;
        });

    });
</script>
</body>
</html>